#ifndef __BSP_INC_H
#define __BSP_INC_H

#ifdef __cplusplus
extern "C"
{
#endif

#include "stm32f1xx.h"
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_conf.h"

#include "sys_Debug.h"
#include "sys_Delay.h"

    typedef enum
    {
        bsp_ok = 0,
        bsp_error = 1,
    } bsp_status;

#define DEMO_LED 1
#define DEMO_SERIAL 1
#define DEMO_FIFO 1
#define DEMO_PWM 1
#define DEMO_IRREMOTE 1

/* TypeDef */
#define BSP_OK HAL_OK
#define BSP_ERROR HAL_ERROR
#define BSP_BUSY HAL_BUSY
#define BSP_TIMEOUT HAL_TIMEOUT

#ifdef HAL_GPIO_MODULE_ENABLED
    typedef GPIO_TypeDef BSP_GPIO_t;
#else
#error "HAL_GPIO_MODULE_DISENABLED"
typedef void BSP_GPIO_t;
#endif

#ifdef HAL_UART_MODULE_ENABLED
    typedef UART_HandleTypeDef BSP_UART_Handle_t;
#else
#error "HAL_UART_MODULE_DISENABLED"
typedef void BSP_UART_Handle_t;
#endif

#ifdef HAL_I2C_MODULE_ENABLED
    typedef I2C_HandleTypeDef BSP_I2C_Handle_t;
#else
#error "HAL_I2C_MODULE_DISENABLED"
typedef void BSP_I2C_Handle_t;
#endif

#ifdef HAL_TIM_MODULE_ENABLED
    typedef TIM_HandleTypeDef BSP_TIM_Handle_t;
#else
#error "HAL_TIM_MODULE_DISENABLED"
typedef void BSP_TIM_Handle_t;
#endif

/* FunctionDef */
#define BSP_Delay_ms(ms) Delay_ms(ms)

#ifdef HAL_GPIO_MODULE_ENABLED
#define BSP_GPIO_Write(GPIOx, GPIO_Pin, State) HAL_GPIO_WritePin(GPIOx, GPIO_Pin, (GPIO_PinState)State)
#else
#error "HAL_GPIO_MODULE_DISENABLED"
#define BSP_GPIO_Write(GPIOx, GPIO_Pin, State) bsp_error
#endif

#ifdef HAL_UART_MODULE_ENABLED
#define BSP_UART_Init(huart) HAL_UART_Init(huart)
#define BSP_UART_Transmit(huart, pData, Size, Timeout) HAL_UART_Transmit(huart, (const uint8_t *)pData, (uint16_t)Size, (uint32_t)Timeout)
#define BSP_UART_Receive_IT(huart, pData, Size) HAL_UART_Receive_IT(huart, (uint8_t *)pData, (uint16_t)Size)
#else
#error "HAL_UART_MODULE_DISENABLED"
#define BSP_UART_Init(huart) bsp_error
#define BSP_UART_Transmit(huart, pData, Size, Timeout) bsp_error
#define BSP_UART_Receive_IT(huart, pData, Size) bsp_error
#define
#endif

#ifdef HAL_I2C_MODULE_ENABLED
#define BSP_I2C_Write(hi2c, Address, pData, Size, Timeout) HAL_I2C_Master_Transmit(hi2c, (uint16_t)Address, (uint8_t *)pData, (uint16_t)Size, Timeout)
#define BSP_I2C_Read(hi2c, Address, Reg, pData, Size, Timeout) HAL_I2C_Mem_Read(hi2c, (uint16_t)Address, (uint16_t)Reg, I2C_MEMADD_SIZE_8BIT, (uint8_t *)pData, (uint16_t)Size, Timeout)
#else
#error "HAL_I2C_MODULE_DISENABLED"
#define BSP_I2C_Write(hi2c, Address, pData, Size, Timeout) bsp_error
#define BSP_I2C_Read(hi2c, Address, Reg, pData, Size, Timeout) bsp_error
#endif

#ifdef HAL_TIM_MODULE_ENABLED
#define BSP_TIM_PWM_Init(htim) HAL_TIM_PWM_Init(htim)
#define BSP_TIM_PWM_Start_IT(htim, channel) HAL_TIM_PWM_Start_IT(htim, channel)
#define BSP_TIM_PWM_Stop_IT(htim, channel) HAL_TIM_PWM_Stop_IT(htim, channel)
#define BSP_TIM_Set_Compare(htim, channel, Compare) __HAL_TIM_SET_COMPARE(htim, channel, Compare)
#else
#error "HAL_TIM_MODULE_ENABLED"
#define BSP_TIM_PWM_Init(htim) bsp_error
#define BSP_TIM_PWM_Start_IT(htim, channel) bsp_error
#define BSP_TIM_PWM_Stop_IT(htim, channel) bsp_error
#define BSP_TIM_Set_Compare(tim, channel, Compare) bsp_error
#endif

#ifdef __cplusplus
}
#endif

#endif
